package com.cris.service;

import com.cris.domain.ItemId;
import com.cris.domain.Register;
import com.cris.helper.DatabaseHelper;
import org.springframework.stereotype.Service;
import java.sql.Timestamp;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by owen on 2017/4/21.
 */
@Service
public class OrderService {
    public void addlineItems(long id,long quantity,double price,String orderNo){
        double sumup = price*quantity;
        Map<String,Object> map = new HashMap<>();
        map.put("quantity",quantity);
        map.put("sumup",sumup);
        map.put("id",id);
        map.put("price",price);
        map.put("orderno",orderNo);
        DatabaseHelper.insertIntoTable(map,"orderitem");
    }
    public boolean addOrderNo(String orderNo){
        Map<String,Object> map = new HashMap<>();
        map.put("orderno",orderNo);
        return DatabaseHelper.insertIntoTable(map,"saleorder");
    }
    public void addOrder(String orderNo,double total){
        Map<String,Object> map = new HashMap<>();
        map.put("time",new Timestamp(System.currentTimeMillis()));
        // TODO: 2017/4/21 handler
        map.put("total",total);
        DatabaseHelper.updateTable("saleorder",map,"orderno",orderNo);
    }
    public void addPayment(double total,String payNo,String orderNo){
        Map<String,Object> map = new HashMap<>();
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String paytime=format.format(date);
        map.put("Paytime",paytime);
        map.put("amount",total);
        map.put("payno",payNo);
        map.put("orderno",orderNo);
        DatabaseHelper.insertIntoTable(map,"payment");
    }
    public double getTotal(Register register){
        return register.getCurrentSale().getTotal().getAmount();
    }
    public double getPrice(Register register, ItemId itemId){
        return register.getCatalog().getProductDescription(itemId).getPrice().getAmount();
    }
    public String getOrderNo(Register register){
        return register.getCurrentSale().getOrderNo();
    }
}


